//
// Created by Administrator on 2023/8/1.
//
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int buyChoco(vector<int> &prices, int money) {
        // 先对prices排序
        sort(prices.begin(), prices.end());
        if (prices[0] + prices[1] > money)
            return money;
        else
            return money - prices[0] - prices[1];
    }
};
class Solution2 {
public:
    int buyChoco(vector<int> &prices, int money) {
        // 搜索prices中最小的两个值
        int min1 = INT_MAX, min2 = INT_MAX;
        for (int price : prices) {
            if (price < min1) {
                min2 = min1;
                min1 = price;
            } else if (price < min2) {
                min2 = price;
            }
        }
        if (min1 + min2 > money)
            return money;
        else
            return money - min1 - min2;
    }
};

int main() {
    Solution s;
    vector<int> prices = {2, 3, 4};
    int money = 5;
    int res = s.buyChoco(prices, money);
    cout << res << endl;
    return 0;
}